package model;

import java.util.ArrayList;
import org.jbox2d.dynamics.ContactListener;
import org.jbox2d.dynamics.contacts.ContactPoint;
import org.jbox2d.dynamics.contacts.ContactResult;
import behaviors.Behavior;

public class Contacts implements ContactListener {

	ArrayList<ContactPoint> contactPoints;
	Entity shapeData1, shapeData2;

	public Contacts() {
		contactPoints = new ArrayList<ContactPoint>();
	}

	@Override
	public void add(ContactPoint arg0) {
		contactPoints.add(arg0);
		shapeData1 = ((Entity) arg0.shape1.getBody().getUserData());
		shapeData2 = ((Entity) arg0.shape2.getBody().getUserData());
		for (Behavior b : shapeData1.behaviorList){
			b.onCollide(arg0);
		}
		for (Behavior b : shapeData2.behaviorList){
			b.onCollide(arg0);
		}
	}

	@Override
	public void persist(ContactPoint arg0) {
		shapeData1 = ((Entity) arg0.shape1.getBody().getUserData());
		shapeData2 = ((Entity) arg0.shape2.getBody().getUserData());
		for (Behavior b : shapeData1.behaviorList){
			b.onPersist(arg0);
		}
		for (Behavior b : shapeData2.behaviorList){
			b.onPersist(arg0);
		}
	}

	@Override
	public void remove(ContactPoint arg0) {
		contactPoints.remove(arg0);
		shapeData1 = ((Entity) arg0.shape1.getBody().getUserData());
		shapeData2 = ((Entity) arg0.shape2.getBody().getUserData());
		for (Behavior b : shapeData1.behaviorList){
			b.onRemove(arg0);
		}
		for (Behavior b : shapeData2.behaviorList){
			b.onRemove(arg0);
		}
	}

	@Override
	public void result(ContactResult arg0) {
		shapeData1 = ((Entity) arg0.shape1.getBody().getUserData());
		shapeData2 = ((Entity) arg0.shape2.getBody().getUserData());
		for (Behavior b : shapeData1.behaviorList){
			b.onResult(arg0);
		}
		for (Behavior b : shapeData2.behaviorList){
			b.onResult(arg0);
		}
	}
}
